package app.util;

import app.service.AppCodeService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Map;

public class FtlUtil {

    private static Logger logger = LoggerFactory.getLogger(FtlUtil.class);

    public static void createOut(Map dataMap, String ftl, String outPath){
        try {
            Configuration configuration = new Configuration(Configuration.getVersion());
            configuration.setDefaultEncoding("UTF-8");
            configuration.setClassicCompatible(true);
            configuration.setClassForTemplateLoading(FtlUtil.class,"/templates");
            File file = new File(outPath);
            if (!file.getParentFile().exists()) {
               file.getParentFile().mkdirs();
            }
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
            Template template = configuration.getTemplate(ftl);
            template.process(dataMap, out);
            out.flush();
            out.close();
        } catch (IOException e) {
            logger.error("程序异常",e);
        } catch (TemplateException e) {
            logger.error("程序异常",e);
        }
    }
}
